package com.dlxj.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.dlxj.entity.Group;
import com.dlxj.entity.Model;
import com.dlxj.entity.User;
import com.dlxj.service.UserService;
import com.dlxj.util.MyJSON;
import com.dlxj.util.PageBean;

public class UserAction {

	UserService userService;
	User user = new User();
	Group group = new Group();
	String json;
	String oldPwd;
	String newPwd;
	String name;
	int uid;
	int state;
	int pageIndex;
	int groupid;

	
	
	public String findgroup(){
		List<Group> groups = userService.findgroup();
		json = MyJSON.toJson(groups).toString();
		return "success";
	}
	
	
	public int getGroupid() {
		return groupid;
	}

	public void setGroupid(int groupid) {
		this.groupid = groupid;
	}

	public String login() {

		int code = userService.login(user);

		json = "{code:" + code + "}";
		return "success";
	}

	public String findModel() {
		User u = getCurrentUser();
		if (u != null) {
			List<Model> models = userService.findModel(u.getId());
			json = MyJSON.toJson(models).toString();
		} else {
			json = "{\"msg\";\"获取失败\"}";
		}
		return "success";
	}

	public String findCurrentUser() {
		User u = getCurrentUser();
		json = MyJSON.toJson(u, "_user", "groups", "users").toString();
		return "success";
	}

	public String updatePassword() {

		int code = userService.updatePassword(getCurrentUser().getId(), oldPwd,
				newPwd);
		json = "{code:" + code + "}";
		return "success";

	}

	public String updatePersonInfo() {
		user.setId(getCurrentUser().getId());
		int code = userService.modifyInfo(user);
		json = "{code:" + code + "}";
		return "success";

	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public String updateUsergroup() {
		group.setId(groupid);
		int code = userService.modifyGroup(uid, group);
		json = "{code:" + code + "}";
		return "success";
	}

	public String updateUserState() {
		int code = userService.modifyState(uid, state);
		json = "{code:" + code + "}";
		return "success";
	}

	public String loginout() {
		userService.loginOut();
		return "success";
	}

	public String addUser() {
		user.setJoindate(new Date());

		user.setState(1);

		int code = userService.addUser(user);

		json = "{code:" + code + "}";

		return "success";

	}

	public String findByGName() {
		List<User> bean = userService.findByGroupName(name);
		json = MyJSON.toJson(bean, "_user", "groups", "users").toString();
		return "success";
	}

	public String deleteUser() {
		int code = userService.deleteUser(uid);
		json = "{code:" + code + "}";
		return "success";
	}

	public String findList() {

		PageBean bean = userService.findUserList(name, pageIndex);
		json = MyJSON.toJson(bean, "_user", "groups", "users").toString();
		return "success";
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	private User getCurrentUser() {
		return (User) ServletActionContext.getRequest().getSession()
				.getAttribute("user");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

}
